package com.zzl.finance.data.service.impl;

import com.zzl.finance.data.constant.SmsConstant;
import com.zzl.finance.data.constant.SmsTypeConstant;
import com.zzl.finance.data.service.CodeService;
import com.zzl.general.common.exception.SysException;
import com.zzl.general.common.third.sms.manager.SmsManager;
import com.zzl.general.common.utils.Result;
import com.zzl.general.common.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author gull
 */
@Service
public class CodeServiceImpl implements CodeService {

    @Autowired
    private SmsManager smsManager;

    @Override
    public void getLoginCode(String phone, String type) throws SysException {
        Result result;
        //法人
        if (SmsTypeConstant.USER_LEGAL_REGISTER.getKey().equals(type)) {
            result = smsManager.sendSms(phone, SmsTypeConstant.USER_LEGAL_REGISTER.getValue());
            //经办人
        } else if (SmsTypeConstant.USER_HANDLER_REGISTER.getKey().equals(type)) {
            result = smsManager.sendSms(phone, SmsTypeConstant.USER_HANDLER_REGISTER.getValue());
        }

        else {
            throw new SysException("短信类型不正确");
        }
        if (null == result || ResultCode.SUCCESS.getCode() != result.getCode()) {
            throw new SysException("短信发送失败");
        }
    }


    @Override
    public Result verificationVoucherLoginCode(String code, String phone) {
        return smsManager.checkVerificationCode(phone, SmsConstant.REGISTER, code);

    }

    @Override
    public Result getVoucherLoginCode(String phone) {
     return    smsManager.sendSms(phone,SmsConstant.REGISTER);
    }


}
